<?php

/**
 * Created by PhpStorm.
 * User: 刘鹏
 * Date: 2018/7/14
 * Time: 12:25
 */
class PosterAction extends Action
{
//http://newtest.mboxone.com/test/Poster/addPoster.json&posterName=LPTest1&posterBack=http://oss.mboxone.com/20180710/image/50/1531196056879792.jpg&qrcodeLocation=10|500&qrcodeSize=180*180&isJoinQuan=1&module=micontent&moduleId=498&status=1
    public function addPoster($id = NULL, $posterName, $posterBack, $qrcodeLocation, $qrcodeSize, $isJoinQuan, $module, $moduleId, $status)
    {
        /** 1. 判断用户是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        /*实例化数据库*/
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $posterModel = M('poster');
        $userPosterModel = M('module_user_poster');
        Session::switchCustomer($customerId);
        $data['posterName'] = $posterName;
        $data['posterBack'] = $posterBack;
        $data['qrcodeLocation'] = $qrcodeLocation;
        $data['qrcodeSize'] = $qrcodeSize;
        $data['isJoinQuan'] = $isJoinQuan;
        $data['module'] = $module;
        $data['moduleId'] = $moduleId;
        $data['isJoinQuan'] = $isJoinQuan;
        $data['status'] = $status;
        $data['cid'] = $customerId;
        if ($id) {
            $data['updateTime	'] = date('Y-m-d H:i:s');
            $result = $posterModel->where(['id' => $id])->save($data);
            $userPosterModel->where(['posterId' => $id])->setField('picUrl','');
        } else {
            $data['creatTime'] = date('Y-m-d H:i:s');
            $data['updateTime'] = date('Y-m-d H:i:s');
            $data['deleted'] = 0;
            $result = $posterModel->add($data);
            $id = $result;
        }
        if (is_numeric($result)) {
            $id ? $msg = '修改成功' : $msg = '添加成功';
            Output::instance()->object([
                'code' => 0,
                'msg' => $msg,
                'id' => $id
            ]);
            return;
        } else {
            $id ? $msg = '修改失败' : $msg = '添加失败';
            Output::instance()->object([
                'code' => 1,
                'msg' => $msg
            ]);
            return;
        }
    }

    public function deletePoster($id)
    {
        /** 1. 判断用户是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /*实例化数据库*/
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $posterModel = M('poster');
        Session::switchCustomer($customerId);
        $where['id'] = $id;
        $data['deleted'] = 1;
        $data['updateTime	'] = date('Y-m-d H:i:s');
        $result = $posterModel->where($where)->save($data);
        if (is_numeric($result)) {
            Output::instance()->object([
                'code' => 0,
                'msg' => '删除成功'
            ]);
            return;
        } else {
            Output::instance()->object([
                'code' => 1,
                'msg' => '删除失败'
            ]);
            return;
        }
    }

    public function posterList($posterName = NULL, $module = NULL, $status = NULL, $startTime = NULL, $endTime = NULL, $start = 0, $limit = 10)
    {

        /** 1. 判断用户是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /*实例化数据库*/
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $posterModel = M('poster');
        $micontentModel = M('micontent');
        $productModel = M('module_product');
        Session::switchCustomer($customerId);
        !empty($posterName) ? $where['poster.posterName'] = array('like', "%$posterName%") : '';
        !empty($module) ? $where['poster.module'] = array('eq', $module) : '';
        !empty($status) ? $where['poster.module'] = array('eq', $status) : '';
        // 是否匹配开始和结束时间
        if ($startTime != '' && $endTime != '') {   // 同时匹配开始和截止时间
            $where['poster.updateTime'] = array(array('egt', $startTime), array('elt', $endTime));
        } else if ($startTime != '') {  // 只匹配开始时间
            $where["poster.updateTime"] = array('egt', $startTime);
        } else if ($endTime != '') {    // 只匹配结束时间
            $where["poster.updateTime"] = array('elt', $endTime);
        }
        $where['poster.deleted'] = array('eq', 0);
        $where['poster.cid'] = array('eq', $customerId);
        $arr = $posterModel->alias('poster')->where($where)->order('updateTime desc')->limit($start,$limit)->select();

        $total = $posterModel->alias('poster')->where($where)->count();
        foreach ($arr as $key => $value) {
            if ($value['module'] == 'micontent') {
                $arr[$key]['moduleName'] = $micontentModel->where(['id' => $value['moduleId']])->getField('name');
            } elseif ($value['module'] == 'product') {
                $arr[$key]['moduleName'] = $productModel->where(['id' => $value['moduleId']])->getField('name');
            }
        }

        (empty($arr)) ? $arr = [] : '';
        Output::instance()->object([
            'code' => 0,
            'data' => $arr,
            'total' => $total
        ]);

    }

    public function posterDetail($id)
    {
        /** 1. 判断用户是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /*实例化数据库*/
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $posterModel = M('poster');
        $micontentModel = M('micontent');
        $productModel = M('module_product');
        Session::switchCustomer($customerId);
        $where['poster.id'] = $id;
        $arr = $posterModel->alias('poster')->where($where)->find();
        if ($arr['module'] == 'micontent') {
            $arr['moduleName'] = $micontentModel->where(['id' => $arr['moduleId']])->getField('title');
        } elseif ($arr['module'] == 'product') {
            $arr['moduleName'] = $productModel->where(['id' => $arr['moduleId']])->getField('name');
        }
        Output::instance()->object([
            'code' => 0,
            'msg' => '获取成功',
            'data' => $arr,
        ]);

    }
}